﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace aboveXWords.GUI
{
  /// <summary>
  /// A letter hold one character.
  /// </summary>
  [Serializable]
  internal class Letter : ResizableSquare
  {
    private readonly StringFormat m_newStringFormat = StringFormat.GenericTypographic;

    public Letter(Square square)
      : this(square, ' ')
    {
    }

    public Letter(Letter letter)
      : this(letter, letter.Character)
    {
    }

    public Letter(Square square, char c)
      : base(square)
    {
      Rectangle = square.Rectangle;
      Character = c;
      BackgroundBrush = Painter.Colors.LetterBackgroundBrush;
      m_newStringFormat.Alignment = StringAlignment.Center;
      m_newStringFormat.LineAlignment = StringAlignment.Center;
    }

    /// <summary>
    /// Sets or gets the character associated with this Letter.
    /// </summary>
    public char Character { get; set; }

    protected internal Brush BackgroundBrush { get; set; }

    /// <summary>
    /// Clear the square and draw the letter and the surrounding square.
    /// </summary>
    /// <param name="graphics"></param>
    protected internal override void Paint(Graphics graphics)
    {
      graphics.FillRectangle(BackgroundBrush, Rectangle);
      base.Paint(graphics);
      graphics.DrawString(Character.ToString(),
                                         Painter.Colors.LetterFont,
                                         Painter.Colors.LetterBrush,
                                         Rectangle, m_newStringFormat);
    }

    public override bool DropOk(Square square)
    {
      var letter = square as Letter;
      if (letter != null)
        return Character == letter.Character;
      return !(square is ResizableSquare);
    }
  }
}